package com.krain.ddbb.activity;


import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;

import com.krain.corelibrary.widget.loadingView.LoadingView;
import com.krain.corelibrary.widget.recyleview.RefreshRecyclerView;
import com.krain.corelibrary.widget.recyleview.implement.OnRecyclerItemListener;
import com.krain.ddbb.R;
import com.krain.ddbb.adapter.UserWorkAdapter;
import com.krain.ddbb.base.BaseActivity;
import com.krain.ddbb.entity.AppApi;
import com.krain.ddbb.entity.BaseEntity;
import com.krain.ddbb.entity.WorkNameBean;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;
import java.util.List;

@EActivity(R.layout.activity_user_work)
public class UserWorkActivity extends BaseActivity {

    @ViewById(R.id.loadView)
    LoadingView mLoadingView;
    @ViewById(R.id.act_user_work_recyleview)
    RefreshRecyclerView mRefreshRecyclerView;
    UserWorkAdapter adapter;

    private List<WorkNameBean> mEntityList;

    private List<String> idsList;


    @AfterViews
    void afterView() {
        idsList = new ArrayList<>();
        mRefreshRecyclerView.showRefreshing(false);
        mRefreshRecyclerView.setIsLoadMore(false);
        getWorkType(mEntityList, mRefreshRecyclerView, mLoadingView);
    }

    @Background
    protected void getWorkType(List<WorkNameBean> mEntityList, RefreshRecyclerView mRefreshRecyclerView, LoadingView loadingView) {
        List<WorkNameBean> bean = AppApi.getsInstance(mContext).getWorkList(app.getmUserinfo().getAccess_token());
        if (bean != null) {
            setAdapter(bean, mRefreshRecyclerView, loadingView);
        }

    }


    @UiThread
    protected void setAdapter(List<WorkNameBean> mEntityList, RefreshRecyclerView mRefreshRecyclerView, LoadingView loadingView) {
        adapter = new UserWorkAdapter(mEntityList, mContext, onItemCheckedChaged);
        mRefreshRecyclerView.setAdapter(adapter);
        adapter.setOnItemListener(new MyRecyclerItemListener(mEntityList));
        loadingView.showContentView(mRefreshRecyclerView);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        showProgressDialog();
        updateWorkerMsgAtBackground();
        return true;
    }


    @Background
    void updateWorkerMsgAtBackground(){
        BaseEntity entity = AppApi.getsInstance(mContext).updateWorkerMsg(app.getmUserinfo().getAccess_token(), adapter.getChecked());
        if (entity != null) {
            finish();
        }
        dismissProgressDialog();
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        MenuItem item = menu.findItem(R.id.action_search);
        item.setIcon(R.mipmap.ic_footprintchecked);
        return true;
    }

    /**
     * 列表的点击事件
     */
    class MyRecyclerItemListener extends OnRecyclerItemListener {
        List<WorkNameBean> mEntityList;

        public MyRecyclerItemListener(List<WorkNameBean> mEntityList) {
            this.mEntityList = mEntityList;
        }

        @Override
        public void onItemClick(int position, int viewType, RecyclerView.ViewHolder holder, View v) {
            UserWorkAdapter.ViewHolder viewHolder = (UserWorkAdapter.ViewHolder) holder;
        }
    }

    public static void jumpTothisActivity(BaseActivity activity) {
        Intent intent = new Intent(activity, UserWorkActivity_.class);
        activity.startActivity(intent);

    }

    @Override
    protected String getToolbarTitle() {
        return "工种";
    }


    UserWorkAdapter.OnItemCheckedChaged onItemCheckedChaged = new UserWorkAdapter.OnItemCheckedChaged() {
        @Override
        public void onCheckedChaged(String id, boolean isChecked) {
            if (isChecked) {
                idsList.add(id);
            } else {
                idsList.remove(id);
            }
        }

    };

}
